package com.simple;

import com.simple.anno.ScanPackage;
import com.simple.anno.impl.AgentClassImpl;
import com.simple.anno.impl.AnnotationHandlerImpl;
import com.simple.util.ReflectionsUtil;

import java.lang.reflect.InvocationTargetException;

public class Init {

    /**
     * 调用此方法初始化框架
     * @param o 此对象的类对象中应该携带ScanPackage注解
     */
    public static void init(Object o) {
        Class<?> aClass = o.getClass();
        ScanPackage scanPackage = aClass.getDeclaredAnnotation(ScanPackage.class);
        if (scanPackage == null) {
            throw new IllegalArgumentException("找不到注解 ".concat(ScanPackage.class.getName()).concat("无法确定扫描目录"));
        }
        ReflectionsUtil.init(scanPackage.packageName());
        try {
            AgentClassImpl.agentClassImpl();
            AnnotationHandlerImpl.annotationHandlerImpl();
        } catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
